package com.lixiaoyi.emp.controller;

import com.lixiaoyi.emp.entity.EmpManager;
import com.lixiaoyi.emp.service.IEmpManagerService;
import com.lixiaoyi.emp.service.impl.EmpManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-12
 */

//@WebServlet(name = "EmpManagerLoginController",value = "/manager/EmpManagerLoginController")

@WebServlet("/manager/EmpManagerLoginController")
public class EmpManagerLoginController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String inputVcode = request.getParameter("inputVcode");


        //2.校验验证码
        String codes = (String)request.getSession().getAttribute("codes");
        if (!inputVcode.isEmpty() && inputVcode.equalsIgnoreCase(codes)){
            //3.调用service层实现登录
            IEmpManagerService empManagerService = new EmpManagerServiceImpl();
            EmpManager empManager = empManagerService.login(username, password);

            //登录成功 存储在session作用域中
            if (empManager!=null){
                HttpSession session = request.getSession();
                session.setAttribute("empManager",empManager);
                //跳转到查询页面
                response.sendRedirect(request.getContextPath() + "/manager/safe/showAllEmpController");


            }else {
                response.sendRedirect(request.getContextPath() + "/login.html");

            }

        }else {
            //登陆失败 跳转到登录页
            response.sendRedirect(request.getContextPath() + "/login.html");
        }



    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
